#include "mystyleditemdelegate.h"

myStyledItemDelegate::myStyledItemDelegate(QObject *parent) : QStyledItemDelegate(parent)
{

}

myStyledItemDelegate::~myStyledItemDelegate()
{

}

void myStyledItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    painter->setRenderHints(QPainter::Antialiasing, true);
    //使用平滑的pixmap变换算法(双线性插值算法)
    painter->setRenderHint(QPainter::SmoothPixmapTransform, true);

    QStringList list = index.data().toString().split("  ");

    QFont font;
    font.setFamily("SimHei");
    font.setPointSize(9);//设置文字大小为9
    font.setBold(true);//设置文字为粗体

    painter->setPen(Qt::black);
    painter->setFont(font);

    QImage image;
    if("晴" == list[2])
    {
        if(false == image.load("太阳.png"))
        {
            qDebug()<<"加载图片失败";
        }
    }
    else if("阴" == list[2])
    {
        if(false == image.load("阴天.png"))
        {
            qDebug()<<"加载图片失败";
        }
    }
    else if("多云" == list[2])
    {
        if(false == image.load("多云.png"))
        {
            qDebug()<<"加载图片失败";
        }
    }
    else if("霾" == list[2])
    {
        if(false == image.load("多雾.png"))
        {
            qDebug()<<"加载图片失败";
        }
    }
    else if(true == list[2].contains("雨"))
    {
        if(false == image.load("下雨.png"))
        {
            qDebug()<<"加载图片失败";
        }
    }
    else if(true == list[2].contains("雪"))
    {
        if(false == image.load("下雪.png"))
        {
            qDebug()<<"加载图片失败";
        }
    }

    QStringList data = list[3].split('-');
    painter->drawText(option.rect.left() + 5,option.rect.top() + 15, data[0] + '.' + data[1] + '.' + data[2]);
    painter->drawImage(QRect(option.rect.left() + option.rect.width() / 2 - 30,option.rect.top() + 25,60,60),image);

    font.setBold(false);
    painter->setFont(font);

    painter->drawText(option.rect.left() + 37,option.rect.top() + 105, list[2]);

    data = list[0].split(' ');
    painter->drawText(option.rect.left() + 15,option.rect.top() + 130, data[0]);
    painter->drawText(option.rect.left() + 35,option.rect.top() + 150, data[1]);

    data = list[1].split(' ');
    painter->drawText(option.rect.left() + 15,option.rect.top() + 180, data[0]);
    painter->drawText(option.rect.left() + 35,option.rect.top() + 200, data[1]);
}
